





package com.lanou3g.springcloud.hello.consumer.web;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

/**
 * 显式调用EurekaClientAPI方式使用Eureka服务注册中心
 */
@Slf4j
@Controller
@RequestMapping("/raw")
public class HelloController {


	@Autowired
	private EurekaClient eurekaClient;

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping("/")
	public String defaultHello(Model model) {
		String url = getBaseServerUrlByEurekaClient();
		String result = restTemplate.getForObject(url, String.class);
		model.addAttribute("msg", result);
		return "index";
	}

	@RequestMapping("/name/{name}")
	public String nameHello(@PathVariable String name, Model model) {
		String url = getBaseServerUrlByEurekaClient()+"/name/"+name;
		String result = restTemplate.getForObject(url, String.class);
		model.addAttribute("msg", result);
		return "index";
	}

	@RequestMapping("/json/{name}")
	public String jsonHello(@PathVariable String name, Model model) {
		String url = getBaseServerUrlByDiscoveryClient()+"/json/"+name;
		String result = restTemplate.getForObject(url, String.class);
		model.addAttribute("msg", result);
		return "index";
	}

	/**
	 * 使用原生netflixAPI从Eureka中获取服务信息
	 */
	private String getBaseServerUrlByEurekaClient() {
		InstanceInfo serverFromEureka = eurekaClient.getNextServerFromEureka("hello-server", false);
		String appName = serverFromEureka.getAppName();
		String ip = serverFromEureka.getIPAddr();
		String host = serverFromEureka.getHostName();
		int port = serverFromEureka.getPort();
		log.info("appName: " + appName + ", ip: " + ip + "host:, " + host + "port: " + port);
		String url = "http://" + host+":" + port;
		return url;
	}

	/**
	 * 使用SpringCloud封装的抽象简化版API从抽象的服务注册中心(不一定是Eureka)中获取服务信息
	 */
	public String getBaseServerUrlByDiscoveryClient() {
		List<ServiceInstance> serviceInstances = discoveryClient.getInstances("hello-server");
		if(serviceInstances == null || serviceInstances.size() < 1) {
			return null;
		}
		int idx = new Random().nextInt(serviceInstances.size());
		String instanceId = serviceInstances.get(idx).getInstanceId();
		String serviceId = serviceInstances.get(idx).getServiceId();
		String host = serviceInstances.get(idx).getHost();
		int port = serviceInstances.get(idx).getPort();
		String scheme = serviceInstances.get(idx).getScheme();
		log.info("scheme: " + scheme + ", host: " + host + ", port: " + port+", instanceId: " + instanceId + ", serviceId: " + serviceId);
		String url = "http://" + host+":" + port;
		return url;
	}
}